﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace MikroStokAlimi
{
    public class Data
    {
        public string Server { get; set; }
        public string Database { get; set; }

        private string GetString()
        {
            return string.Format("Data Source={0};Database={1};Trusted_Connection=True;", this.Server, this.Database);
        }

        public DataTable List(string cmdText)
        {
            DataTable sonuc = new DataTable();
            using (SqlConnection cn = new SqlConnection(GetString()))
            {
                using (SqlCommand cmd = new SqlCommand(cmdText, cn))
                {
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        try
                        {
                            adp.Fill(sonuc);
                        }
                        catch (Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                    }
                }
                cn.Close();
            }
            return sonuc;
        }

        public bool ExecQuery(string cmdText, params SqlParameter[] prm)
        {
            bool sonuc = false;
            using (SqlConnection cn = new SqlConnection(GetString()))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(cmdText, cn))
                {
                    try
                    {
                        cmd.Parameters.AddRange(prm);
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        sonuc = true;
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                        sonuc = false;
                    }
                }
                cn.Close();
            }
            return sonuc;
        }

        public bool ExecQuery(string cmdText)
        {
            bool sonuc = false;
            using (SqlConnection cn = new SqlConnection(GetString()))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(cmdText, cn))
                {
                    try
                    {
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        sonuc = true;
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                        sonuc = false;
                    }
                }
                cn.Close();
            }
            return sonuc;
        }

        private Data() { }
        static Data _instance = null;
        public static Data Get
        {
            get
            {
                if (_instance == null)
                    _instance = new Data();
                return _instance;
            }
        }


    }
}
